package cn.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.common.ExceResult;
import cn.common.GridCommon;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.sys.OrgEntity;
import cn.kpi.entity.sys.RoleEntity;
import cn.kpi.service.sys.IOrgService;

@Controller
@RequestMapping("/org")
public class OrgController extends BaseController {

	@Autowired
	private IOrgService  iOrgService;
	
	@RequestMapping(value = "/org_list")
	public String OrgList(Model model) {
		try
		{
			List<OrgEntity> lst = iOrgService.GetOrgListByWhere(" AND orgtype=0 ");
			for (int i = 0; i < lst.size(); i++) {
				String html="";
				for(int j=0;j<lst.get(i).getLevel();j++){
					html+="&#12288;";
				}
				lst.get(i).setOrgname(html+lst.get(i).getOrgname());
			}
			model.addAttribute("lst", lst);
		}
		catch(Exception ex){
			
		}
		return "sys/org_list";
	}
	
	@RequestMapping(value = "/group_list")
	public String GroupList(Model model) {
		try
		{
			String where=" AND orgtype=0 ";
			where+=" AND orgid IN("+this.GetOrgListByRole()+") ";
			
			List<OrgEntity> lst = iOrgService.GetOrgListByWhere(where);
			for (int i = 0; i < lst.size(); i++) {
				String html="";
				for(int j=0;j<lst.get(i).getLevel();j++){
					html+="&#12288;";
				}
				lst.get(i).setOrgname(html+lst.get(i).getOrgname());
			}
			model.addAttribute("lst", lst);
		}
		catch(Exception ex){
			
		}
		return "sys/group_list";
	}
	
	@RequestMapping(value = "/group_user")
	public String GroupUser(Model model,Long groupid,Long orgparentid) {
		try
		{
			
			model.addAttribute("groupid", groupid);
			model.addAttribute("orgparentid", orgparentid);
		}
		catch(Exception ex){
			
		}
		return "sys/group_user";
	}
	
	@RequestMapping(value = "/add_org")
	public String AddOrg(HttpServletRequest request,HttpServletResponse response){
		try{
			OrgEntity entity=new OrgEntity();
			entity.setOrgid(Long.parseLong(request.getParameter("orgid")));
			entity.setRemark(request.getParameter("remark"));
			entity.setOrgname(request.getParameter("orgname").trim());
			//entity.setOrgcode(request.getParameter("orgcode").trim());
			//System.out.println(request.getParameter("orderindex"));
			entity.setOrderindex(Integer.parseInt(request.getParameter("orderindex")));
			entity.setOrgtype(0);
			
			int iResult=0;
			if(entity.getOrgid()>0){
				iResult=iOrgService.UpdateEntity(entity);
			}
			else{
				entity.setOrgparentid(Long.parseLong(request.getParameter("parentid")));
				iResult=iOrgService.InsertEntity(entity);
			}
		
			return renderStringScript(response,"<script>window.parent.CloseDialog("+iResult+")</script>");
		}
		catch(Exception ex){
			ex.printStackTrace();
			return renderStringScript(response,"<script>window.parent.CloseDialog(0)</script>");
		}
	}
	
	@RequestMapping(value = "/add_group")
	public String AddGroup(HttpServletRequest request,HttpServletResponse response){
		try{
			OrgEntity entity=new OrgEntity();
			entity.setOrgid(Long.parseLong(request.getParameter("orgid")));
			entity.setRemark(request.getParameter("remark"));
			entity.setOrgname(request.getParameter("orgname").trim());
			entity.setOrderindex(Integer.parseInt(request.getParameter("orderindex")));
			entity.setOrderindex(0);
			entity.setOrgtype(1);
			
	        
			int iResult=0;
			if(entity.getOrgid()>0){
				iResult=iOrgService.UpdateEntity(entity);
			}
			else{
				entity.setOrgparentid(Long.parseLong(request.getParameter("parentid")));
				//System.out.println(JSON.toJSONString(entity));
				iResult=iOrgService.InsertEntity(entity);
				
			}
			
		
			return renderStringScript(response,"<script>window.parent.CloseDialog("+iResult+")</script>");
		}
		catch(Exception ex){
			ex.printStackTrace();
			return renderStringScript(response,"<script>window.parent.CloseDialog(0)</script>");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete_org")
	public ExceResult DeleteOrg(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long orgid=Long.parseLong(request.getParameter("orgid"));
		 int iResult=iOrgService.DeleteEntity(orgid);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
//	@LoginCheck(description = true)
//	@ResponseBody
//	@RequestMapping(value = "/get_list")
//	public LayTable<OrgEntity> GetList(HttpServletRequest request) {
//
//		List<OrgEntity> lst=null;
//		LayTable<OrgEntity> layTable=new LayTable<OrgEntity>();
//		try {
//			String RoleName = request.getParameter("rolename");
//			
//			
//			lst = iOrgService.GetOrgList();
//			for (int i = 0; i < lst.size(); i++) {
//				lst.get(i).setRowNum(i + 1);
//			}
//
//			layTable.setCode(0);
//			layTable.setCount(100);
//			layTable.setData(lst);
//			
//		} catch (Exception e) {
//			layTable.setCode(500);
//		}
//		return layTable;
//	}
	
	@ResponseBody
	@RequestMapping(value = "/get_list")
	public GridCommon<OrgEntity> GetList(HttpServletRequest request) {
		GridCommon<OrgEntity> countlist = new GridCommon<OrgEntity>();
		try {
			String RoleName = request.getParameter("rolename");

			List<OrgEntity> list = iOrgService.GetOrgList();
			for (int i = 0; i < list.size(); i++) {
				list.get(i).setRowNum(i + 1);
			}
			// countlist.setCountRecord();
			countlist.setCurrentPage(1);
			countlist.setPageSize(10);
			countlist.setItems(list);
			countlist.setStatus(0);
			countlist.setMsg("");
		} catch (Exception e) {
			countlist.setCurrentPage(1);
			countlist.setPageSize(10);
			countlist.setItems(null);
			countlist.setStatus(1);
			countlist.setMsg("操作成功");
		}
		return countlist;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/get_list_group")
	public GridCommon<OrgEntity> GetListGroup(HttpServletRequest request) {
		GridCommon<OrgEntity> countlist = new GridCommon<OrgEntity>();
		try {
			String RoleName = request.getParameter("rolename");
			String where="";

			where+=" AND orgid IN("+this.GetOrgListByRole()+") OR orgparentid IN ("+this.GetOrgListByRole()+")";
			
			List<OrgEntity> list = iOrgService.GetOrgListByWhere(where);
			for (int i = 0; i < list.size(); i++) {
				list.get(i).setRowNum(i + 1);
			}
			// countlist.setCountRecord();
			countlist.setCurrentPage(1);
			countlist.setPageSize(10);
			countlist.setItems(list);
			countlist.setStatus(0);
			countlist.setMsg("");
		} catch (Exception e) {
			countlist.setCurrentPage(1);
			countlist.setPageSize(10);
			countlist.setItems(null);
			countlist.setStatus(1);
			countlist.setMsg("操作成功");
		}
		return countlist;
	}
}
